'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:UserSettingCtrl
 * @description
 * # UserSettingCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('UserSettingCtrl', function ($scope, UserInfo, $location, RongYService, Util, DataService, openApi, DeveiceIdService, FileService) {
        var clientType = Util.getPlatform() == "android" ? 1 : 2;
        var param = {};
        param.clientType = clientType;
        param.noloader = true;
        $scope.gosection = function (type) {
            var path;
            switch (type) {
                case 1:
                    path = "user/changepswd"
                    break;
                case 2:
                    path = "user/aboutJG"
                    break;
                case 3:
                    path = "user/feedback"
                    break;
            }
            $location.path(path);
        };
        $scope.goSetting = function () {
            $location.path("chat/setting");
        };
        $scope.gocustomService = function () {
            $location.path("work/custSvc");
        };
        $scope.logout = function () {
            DeveiceIdService.cleanDeviceId();
            RongYService.clearConversations()//清除消息会话
            Util.logoutUser(Util.logout);
        };
        $scope.switchNotice = function () {
            DataService.getData(openApi.setPushStatus, param).then(function (data) {
                $scope.checked = data == "opend" ? true : false;
            });
        };
        $scope.uploadavtar = function () {
            var cameraSuccess = function (imageData) {
                var param = {}
                param.portrait = imageData
                FileService.upload(imageData, openApi.settingportrait).then(function (data) {
                    $scope.UserInfo.imgUrl = data;
                    UserInfo.setUseinfo($scope.UserInfo)
                    Util.Toast("设置头像成功")
                });
            };
            var choosePic = function (btn) {
                if (btn == 0) {
                    return
                }
                var cameraOptions = {
                    quality: 30,
                    EncodingType: 0,
                    destinationType: 1
                };
                cameraOptions.sourceType = btn
                navigator.camera.getPicture(cameraSuccess, function () {
                }, cameraOptions);
            }
            navigator.notification.confirm(
                '选择方式',
                choosePic,
                '上传头像', ['拍照', '相册']
            )
        }
        DeveiceIdService.getDeviceId().then(function (id) {
            param.clientId = id;
            init(id)
        })
        var init = function (id) {
            DataService.getData(openApi.getPushStatus, param).then(function (data) {
                $scope.checked = data == "opend" ? true : false;
            });
        }
        UserInfo.getUseinfo().then(function (data) {
            $scope.UserInfo = data;
        });

    });
